package com.example.module_service.device

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.park2212.patrols.DateAdapter
import com.bawei.park2212.patrols.TaskAdapter
import com.blankj.utilcode.util.TimeUtils
import com.example.lib_base.path.RouterPath
import com.example.lib_common.view.entity.DateBean
import com.example.lib_common.view.view.BaseMvvmActivity
import com.example.module_service.R
import com.example.module_service.databinding.ActivityTaskBinding
import com.example.module_service.viewmodel.ServerViewModel
import dagger.hilt.android.AndroidEntryPoint
import java.util.Calendar

@AndroidEntryPoint
@Route(path = RouterPath.PATH_TASK_PAGE)
class TaskActivity : BaseMvvmActivity<ActivityTaskBinding>() {

    private val viewModel :ServerViewModel by viewModels()

    val dateAdapter by lazy { DateAdapter() }
    val taskAdapter by lazy { TaskAdapter() }
    override fun getLayoutId(): Int {
        return R.layout.activity_task
    }

    override fun initViews() {
        super.initViews()
        var calendar = Calendar.getInstance()
        //获取当前时间是星期几
        val week = calendar.get(Calendar.DAY_OF_WEEK)

        //创建一个 dateList 列表用于存储每天的日期信息。
        //循环从 -(week-1) 到 -(week-1)+6，共循环7次，分别对应本周的每一天。
        //对于每一个 i 值，新建一个日历实例 dayCalendar 并将 calendar 的时间设置为 i 天后的时间。
        //获取该天的日期和星期信息，并构建 DateBean 对象添加进 dateList 列表中。
        //如果该天是当天，则标记为 true，否则标记为 false
        val list = mutableListOf<DateBean>()
        for (i in -(week-1)..-(week-1)+6){
            calendar = Calendar.getInstance()
            calendar.add(Calendar.DAY_OF_MONTH,i)
            val day = calendar.get(Calendar.DAY_OF_MONTH)
            val week1 = TimeUtils.getChineseWeek(calendar.time).substring(1,2)
            list.add(DateBean(week1,day,if (i==0) true else false))
        }
        //获取当前年月日
        val date = TimeUtils.getNowString().substring(0,10)
        val date1 = date.substring(5,10)
        mDataBinding.datetime.text = date1
        Log.i("===",list.toString())
        dateAdapter.addData(list)
        mDataBinding.taskRvdate.apply {
            adapter = dateAdapter
            layoutManager = GridLayoutManager(this@TaskActivity,7)
        }

        mDataBinding.taskRv.apply {
            adapter = taskAdapter
            layoutManager = LinearLayoutManager(this@TaskActivity)
        }

        taskAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build(RouterPath.PATH_PATROL_IDETAIL_PAGE)
                .withString("tid",taskAdapter.data[position].id.toString())
                .navigation()
        }
    }

    override fun loadData() {
        super.loadData()
        viewModel.getPatrolTaskBySuccess.observe(this){
            taskAdapter.data.clear()
            taskAdapter.addData(it)
            taskAdapter.notifyDataSetChanged()
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.getPatrolTaskByStatus("0")
    }

}